package com.yonyougov.devops.cruxcisupport.envversionhistory;

import com.yonyougov.devops.cruxcisupport.envversionhistory.dto.EnvVersionHistoryPageLikeRequestDto;
import com.yonyougov.devops.cruxcisupport.envversionhistory.vo.EnvVersionHistoryPageLikeDto;
import com.yonyougov.devops.cruxcisupport.envversionhistory.vo.EnvVersionHistoryPageLikeVo;
import com.yonyougov.devops.fwc.api.dto.BaseResponseDto;
import com.yonyougov.devops.fwc.core.controller.CrudController;
import com.yonyougov.devops.fwc.core.entity.Pagination;
import com.yonyougov.devops.fwc.core.service.BaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.math.BigInteger;

/**
 * Created by ligson on 2017/8/11.
 */
@Api(value = "版本历史模块", tags = "版本历史模块")
@RestController
@RequestMapping("/api/v2/envVersionHistory")
public class EnvVersionHistoryController extends CrudController<EnvVersionHistoryEntity, BigInteger> {

    private final EnvVersionHistoryService envVersionHistoryService;

    public EnvVersionHistoryController(EnvVersionHistoryService envVersionHistoryService) {
        this.envVersionHistoryService = envVersionHistoryService;
    }

    @Override
    public BaseService<EnvVersionHistoryEntity, BigInteger> getService() {
        return envVersionHistoryService;
    }

    //<!--用户自定义代码开始-->

    @ApiOperation(value = "历史模块->版本历史", notes = "根据环境id和应用id以及版本名称分页模糊查")
    @PostMapping("/vh/singele/page/like")
    public BaseResponseDto findSinglePageLike(@RequestBody @Valid EnvVersionHistoryPageLikeRequestDto request) {
        EnvVersionHistoryPageLikeDto envVersionHistoryPageLikeDto = new EnvVersionHistoryPageLikeDto();
        BeanUtils.copyProperties(request, envVersionHistoryPageLikeDto);

        Pagination<EnvVersionHistoryPageLikeVo> pagination = envVersionHistoryService.findByEnvVersionHistoryPageLike(envVersionHistoryPageLikeDto);

        BaseResponseDto result = BaseResponseDto.getSuccessInstance();
        result.put("totalCount", pagination.getTotalCount());
        result.put("data", pagination.getDatas());
        return result;
    }

    //<!--用户自定义代码结束-->
}
